using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace GameFramework.Timer
{
    public class ProcessTimer : GameComponent
    {
        private double _processTime;
        private double _totalTime;

        public event EventHandler<EventArgs> TimeOut;

        public bool IsActive;

        public ProcessTimer(Game game)
            : base(game)
        {
            Reset(0);
            IsActive = false;
        }

        public override void Update(GameTime gameTime)
        {
            if (!IsActive)
                return;
            _processTime += gameTime.ElapsedGameTime.TotalMilliseconds;
            if (_processTime >= _totalTime)
            {
                IsActive = false;
                if (TimeOut != null)
                {
                    TimeOut(this, new EventArgs());
                }
            }
            base.Update(gameTime);
        }

        public void Reset(double duration)
        {
            _processTime = 0;
            _totalTime = duration;
            IsActive = true;
        }

        public void Active(double duration)
        {
            _processTime = 0;
            _totalTime = duration;
            IsActive = true;
        }
    }
}
